local guancheng = fk.CreateSkill {
  name = "lingling__guancheng",
}

Fk:loadTranslationTable{
  ["lingling__guancheng"] = "灌城",
  [":lingling__guancheng"] = "你或你攻击范围内的其他角色弃牌阶段开始时，你可以弃置一张牌或重铸一张红桃牌，并令其摸两张牌，然后若其弃置了至少三张牌，其弃置装备区所有牌并失去1点体力。",
  ["lingling__guancheng_active"] = "灌城",
  ["lingling__guancheng_discard"] = "弃置此牌",
  ["lingling__guancheng_recast"] = "重铸此牌",
  ["#lingling__guancheng-invoke"] = "灌城：你可以令 %dest 摸两张牌，若其弃置了至少三张牌，其弃置装备区所有牌并失去1点体力。",
}

guancheng:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(guancheng.name) and (player == target or player:inMyAttackRange(target)) and target.phase == Player.Discard
  end,
  on_cost = function(self, event, target, player, data)
    local bool, dat = player.room:askToUseActiveSkill(player, {
      skill_name = "lingling__guancheng_active",
      prompt = "#lingling__guancheng-invoke::" .. target.id,
      skip = true,
    })
    if bool and dat then
      p(dat)
      event:setCostData(self, { cards = dat.cards, choice = dat.interaction })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    if event:getCostData(self).choice == "lingling__guancheng_discard" then
      room:throwCard(cards,guancheng.name,player)
    else
      room:recastCard(cards,player,guancheng.name)
    end
    target:drawCards(2, guancheng.name)
    room:setPlayerMark(target, "_lingling__guancheng-phase", 1)
  end
})
guancheng:addEffect(fk.EventPhaseEnd, {
  is_delay_effect = true,
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and not player.dead and player.phase == Player.Discard and player:getMark("_lingling__guancheng-phase") ~= 0 then
      local n = 0
      local logic = player.room.logic
      logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from == player and move.moveReason == fk.ReasonDiscard and move.skillName == "phase_discard" then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                n = n + 1
              end
            end
            if n >= 3 then return true end
          end
        end
      end, Player.HistoryPhase)
      return n >= 3
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(player:getCardIds("e"), guancheng.name, player, player)
    room:loseHp(player, 1, guancheng.name)
  end
})

return guancheng
